package com.itheima.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    /**
     * 分页查询所有员工
     *
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageBean selectAllEmp(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        PageHelper.startPage(page , pageSize);
        Page<Emp> page1 = (Page<Emp>) empMapper.selectAllEmp(name , gender , begin , end);
//        long total = empMapper.getCounts();
//        Integer index = (page - 1) * pageSize;
//        List<Emp> empList = empMapper.selectAllEmp(index , pageSize);
        PageBean pageBean = new PageBean(page1.getTotal() , page1.getResult());
        return pageBean;
    }

    /**
     * 删除员工
     *
     * @param ids
     */
    @Override
    @Log
    public void deleteEmpByIds(List<Integer> ids) {
        empMapper.deleteEmpByIds(ids);
    }

    @Transactional
    @Log
    public void deleteEmpById(Integer id) {
        empMapper.deleteEmpById(id);
    }

    /**
     * 新增员工
     *
     * @param emp
     */
    @Override
    @Log
    public void insertEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insertEmp(emp);
    }

    /**
     * 根据id查找用户,作为数据回显
     *
     * @param id
     * @return emp
     */
    @Override
    public Emp selectEmpById(Integer id) {
        Emp emp = empMapper.selectEmpById(id);
        return emp;
    }

    /**
     * 修改员工信息
     *
     * @param emp
     */
    @Override
    @Log
    public void updateEmpInfo(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmpInfo(emp);
    }

    /**
     * 登录
     *
     * @param emp
     * @return
     */
    @Override
    public Emp login(Emp emp) {
        Emp e = empMapper.login(emp);
        return e;
    }


}
